/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viweb.storemanager.entity;

import com.viweb.storemanager.core.PropertyIndex;
import com.viweb.storemanager.core.SafeObject;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Locale;

/**
 *
 * @author nhchung
 */
public class Product extends BaseEntity implements Comparable, Cloneable, SafeObject, PropertyIndex {

    //khai báo biến
    protected Object cloner;
    private String name;
    private int number;
    private WareHouse warehouse;
    private double price;
    private String unit;
    private Category category;

    //khai báo hàm khởi tạo
    public Product() {
    }

    public Product(int id, String name) {
        setId(id);
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) throws Exception {
        this.number = number;
    }

    /**
     * @return the warehouse
     */
    public WareHouse getWarehouse() {
        return warehouse;
    }

    /**
     * @param warehouse the warehouse to set
     */
    public void setWarehouse(WareHouse warehouse) {
        this.warehouse = warehouse;
    }

    public double getPrice() {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(double price) throws Exception {
        this.price = price;
    }

    /**
     * @return the unit
     */
    public String getUnit() {
        return unit;
    }

    /**
     * @param unit the unit to set
     */
    public void setUnit(String unit) {
        this.unit = unit;
    }

    /**
     * @return the category
     */
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Object getCloner() {
        return this.cloner;
    }

    public boolean validate() {
        if (this.getWarehouse() == null) {
            this.setValidDescription("Chưa chọn kho chứa sản phẩm.");
            return false;
        }

        if (this.getCategory() == null) {
            this.setValidDescription("Chưa chọn loại sản phẩm");
            return false;
        }
        return true;
    }
    //khai báo phương thức

    public Object getPropertyValue(int index) {
        String value = "";
        switch (index) {
            case 1:
                value = String.valueOf(getId());
                break;
            case 2:
                value = getName();
                break;
            case 3:
                value = String.valueOf(getNumber());
                break;
            case 4:
                value = getWarehouse().getName();
                break;
            case 5:
                value = String.valueOf(getPrice());
                break;
            case 6:
                value = getUnit();
                break;
            case 7:
                value = getCategory().getName();
                break;
            case 8:
                value = getDatemodifies().toString();
                break;
            case 9:
                value = String.valueOf(getStatus());
                break;
            case 10:
                value = getNote();
                break;
        }
        return value;
    }

    public void setPropertyValue(int index, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Class getPropertyClass(int index) {
        return String.class;
    }

    public int compareTo(Object object) {
        if (!(object instanceof Product)) {
            throw new ClassCastException("Ngoại lệ: đối tượng thức uống không hợp lệ!");
        }

        RuleBasedCollator viCollator = (RuleBasedCollator) Collator.getInstance(new Locale("vi"));
        return viCollator.compare(this.getName(), ((Product) object).getName());

    }

    public void backup() {
        try {
            this.cloner = this.clone();
        } catch (CloneNotSupportedException ex) {
        }
    }

    public void restore() {
        try {
            this.setName(((Product) getCloner()).getName());
            this.setNumber(((Product) getCloner()).getNumber());
            this.setPrice(((Product) getCloner()).getPrice());
            this.setWarehouse(((Product) getCloner()).getWarehouse());
            this.setCategory(((Product) getCloner()).getCategory());
        } catch (Exception ex) {
        }
    }

    @Override
    public String toString() {
        return this.getName();
    }
}
